<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="简介stdio .h 头文件定义了三个变量类型、一些宏和各种函数来执行输入和输出。 库变量下面是头文件 stdio.h 中定义的变量类型：    序号 变量 &amp;amp; 描述     1 size_t  这是无符号整数类型，它是 sizeof 关键字的结果。   2 FILE  这是一个适合存储文件流信息的对象类型。   3 fpos_t  这是一个适合存储文件中任何位置的对象类型。    库宏下">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="stdio.h">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2015/03/12/cfunc12/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="简介stdio .h 头文件定义了三个变量类型、一些宏和各种函数来执行输入和输出。 库变量下面是头文件 stdio.h 中定义的变量类型：    序号 变量 &amp;amp; 描述     1 size_t  这是无符号整数类型，它是 sizeof 关键字的结果。   2 FILE  这是一个适合存储文件流信息的对象类型。   3 fpos_t  这是一个适合存储文件中任何位置的对象类型。    库宏下">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-01-20T02:54:01.391Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="stdio.h">
<meta name="twitter:description" content="简介stdio .h 头文件定义了三个变量类型、一些宏和各种函数来执行输入和输出。 库变量下面是头文件 stdio.h 中定义的变量类型：    序号 变量 &amp;amp; 描述     1 size_t  这是无符号整数类型，它是 sizeof 关键字的结果。   2 FILE  这是一个适合存储文件流信息的对象类型。   3 fpos_t  这是一个适合存储文件中任何位置的对象类型。    库宏下">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2015/03/12/cfunc12/">





  <title>stdio.h | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2015/03/12/cfunc12/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">stdio.h</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2015-03-12T20:35:45+08:00">
                2015-03-12
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/C函数库/" itemprop="url" rel="index">
                    <span itemprop="name">C函数库</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>stdio .h 头文件定义了三个变量类型、一些宏和各种函数来执行输入和输出。</p>
<h2 id="库变量"><a href="#库变量" class="headerlink" title="库变量"></a>库变量</h2><p>下面是头文件 stdio.h 中定义的变量类型：</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>变量 &amp; 描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><strong>size_t</strong> <br> 这是无符号整数类型，它是 sizeof 关键字的结果。</td>
</tr>
<tr>
<td>2</td>
<td><strong>FILE</strong> <br> 这是一个适合存储文件流信息的对象类型。</td>
</tr>
<tr>
<td>3</td>
<td><strong>fpos_t</strong> <br> 这是一个适合存储文件中任何位置的对象类型。</td>
</tr>
</tbody>
</table>
<h2 id="库宏"><a href="#库宏" class="headerlink" title="库宏"></a>库宏</h2><p>下面是头文件 stdio.h 中定义的宏：</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>宏 &amp; 描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><strong>NULL</strong> <br> 这个宏是一个空指针常量的值。</td>
</tr>
<tr>
<td>2</td>
<td><strong>_IOFBF、_IOLBF 和 _IONBF</strong> <br> 这些宏扩展了带有特定值的整型常量表达式，并适用于 setvbuf 函数的第三个参数。</td>
</tr>
<tr>
<td>3</td>
<td><strong>BUFSIZ</strong> <br> 这个宏是一个整数，该整数代表了 setbuf 函数使用的缓冲区大小。</td>
</tr>
<tr>
<td>4</td>
<td><strong>EOF</strong> <br> 这个宏是一个表示已经到达文件结束的负整数。</td>
</tr>
<tr>
<td>5</td>
<td><strong>FOPEN_MAX</strong> <br> 这个宏是一个整数，该整数代表了系统可以同时打开的文件数量。</td>
</tr>
<tr>
<td>6</td>
<td><strong>FILENAME_MAX</strong> <br> 这个宏是一个整数，该整数代表了字符数组可以存储的文件名的最大长度。如果实现没有任何限制，则该值应为推荐的最大值。</td>
</tr>
<tr>
<td>7</td>
<td><strong>L_tmpnam</strong> <br> 这个宏是一个整数，该整数代表了字符数组可以存储的由 tmpnam 函数创建的临时文件名的最大长度。</td>
</tr>
<tr>
<td>8</td>
<td><strong>SEEK_CUR、SEEK_END 和 SEEK_SET</strong> <br> 这些宏是在 fseek 函数中使用，用于在一个文件中定位不同的位置。</td>
</tr>
<tr>
<td>9</td>
<td><strong>TMP_MAX</strong> <br> 这个宏是 tmpnam 函数可生成的独特文件名的最大数量。</td>
</tr>
<tr>
<td>10</td>
<td><strong>stderr、stdin 和 stdout</strong> <br> 这些宏是指向 FILE 类型的指针，分别对应于标准错误、标准输入和标准输出流。</td>
</tr>
</tbody>
</table>
<h2 id="库函数"><a href="#库函数" class="headerlink" title="库函数"></a>库函数</h2><p>下面是头文件 stdio.h 中定义的函数：</p>
<p>为了更好地理解函数，请按照下面的序列学习这些函数，因为第一个函数中创建的文件会在后续的函数中使用到。</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>函数 &amp; 描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><strong>int fclose(FILE &#42;stream)</strong> <br> 关闭流 stream。刷新所有的缓冲区。</td>
</tr>
<tr>
<td>2</td>
<td><strong>void clearerr(FILE &#42;stream)</strong> <br> 清除给定流 stream 的文件结束和错误标识符。</td>
</tr>
<tr>
<td>3</td>
<td><strong>int feof(FILE &#42;stream)</strong> <br> 测试给定流 stream 的文件结束标识符。</td>
</tr>
<tr>
<td>4</td>
<td><strong>int ferror(FILE &#42;stream)</strong> <br> 测试给定流 stream 的错误标识符。</td>
</tr>
<tr>
<td>5</td>
<td><strong>int fflush(FILE &#42;stream)</strong> <br> 刷新流 stream 的输出缓冲区。</td>
</tr>
<tr>
<td>6</td>
<td><strong>int fgetpos(FILE &#42;stream, fpos_t &#42;pos)</strong> <br> 获取流 stream 的当前文件位置，并把它写入到 pos。</td>
</tr>
<tr>
<td>7</td>
<td><strong>FILE &#42;fopen(const char &#42;filename, const char &#42;mode)</strong> <br> 使用给定的模式 mode 打开 filename 所指向的文件。</td>
</tr>
<tr>
<td>8</td>
<td><strong>size_t fread(void &#42;ptr, size_t size, size_t nmemb, FILE &#42;stream)</strong> <br> 从给定流 stream 读取数据到 ptr 所指向的数组中。</td>
</tr>
<tr>
<td>9</td>
<td><strong>FILE &#42;freopen(const char &#42;filename, const char &#42;mode, FILE &#42;stream)</strong> <br> 把一个新的文件名 filename 与给定的打开的流 stream 关联，同时关闭流中的旧文件。</td>
</tr>
<tr>
<td>10</td>
<td><strong>int fseek(FILE &#42;stream, long int offset, int whence)</strong> <br> 设置流 stream 的文件位置为给定的偏移 offset，参数 offset 意味着从给定的 whence 位置查找的字节数。</td>
</tr>
<tr>
<td>11</td>
<td><strong>int fsetpos(FILE &#42;stream, const fpos_t &#42;pos)</strong> <br> 设置给定流 stream 的文件位置为给定的位置。参数 pos 是由函数 fgetpos 给定的位置。</td>
</tr>
<tr>
<td>12</td>
<td><strong>long int ftell(FILE &#42;stream)</strong> <br> 返回给定流 stream 的当前文件位置。</td>
</tr>
<tr>
<td>13</td>
<td><strong>size_t fwrite(const void &#42;ptr, size_t size, size_t nmemb, FILE &#42;stream)</strong> <br> 把 ptr 所指向的数组中的数据写入到给定流 stream 中。</td>
</tr>
<tr>
<td>14</td>
<td><strong>int remove(const char &#42;filename)</strong> <br> 删除给定的文件名 filename，以便它不再被访问。</td>
</tr>
<tr>
<td>15</td>
<td><strong>int rename(const char &#42;old_filename, const char &#42;new_filename)</strong> <br> 把 old_filename 所指向的文件名改为 new_filename。</td>
</tr>
<tr>
<td>16</td>
<td><strong>void rewind(FILE &#42;stream)</strong> <br> 设置文件位置为给定流 stream 的文件的开头。</td>
</tr>
<tr>
<td>17</td>
<td><strong>void setbuf(FILE &#42;stream, char &#42;buffer)</strong> <br> 定义流 stream 应如何缓冲。</td>
</tr>
<tr>
<td>18</td>
<td><strong>int setvbuf(FILE &#42;stream, char &#42;buffer, int mode, size_t size)</strong> <br> 另一个定义流 stream 应如何缓冲的函数。</td>
</tr>
<tr>
<td>19</td>
<td><strong>FILE &#42;tmpfile(void)</strong> <br> 以二进制更新模式(wb+)创建临时文件。</td>
</tr>
<tr>
<td>20</td>
<td><strong>char &#42;tmpnam(char &#42;str)</strong> <br> 生成并返回一个有效的临时文件名，该文件名之前是不存在的。</td>
</tr>
<tr>
<td>21</td>
<td><strong>int fprintf(FILE &#42;stream, const char &#42;format, …)</strong> <br> 发送格式化输出到流 stream 中。</td>
</tr>
<tr>
<td>22</td>
<td><strong>int printf(const char &#42;format, …)</strong> <br> 发送格式化输出到标准输出 stdout。</td>
</tr>
<tr>
<td>23</td>
<td><strong>int sprintf(char &#42;str, const char &#42;format, …)</strong> <br> 发送格式化输出到字符串。</td>
</tr>
<tr>
<td>24</td>
<td><strong>int vfprintf(FILE &#42;stream, const char &#42;format, va_list arg)</strong> <br> 使用参数列表发送格式化输出到流 stream 中。</td>
</tr>
<tr>
<td>25</td>
<td><strong>int vprintf(const char &#42;format, va_list arg)</strong> <br> 使用参数列表发送格式化输出到标准输出 stdout。</td>
</tr>
<tr>
<td>26</td>
<td><strong>int vsprintf(char &#42;str, const char &#42;format, va_list arg)</strong> <br> 使用参数列表发送格式化输出到字符串。</td>
</tr>
<tr>
<td>27</td>
<td><strong>int fscanf(FILE &#42;stream, const char &#42;format, …)</strong> <br> 从流 stream 读取格式化输入。</td>
</tr>
<tr>
<td>28</td>
<td><strong>int scanf(const char &#42;format, …)</strong> <br> 从标准输入 stdin 读取格式化输入。</td>
</tr>
<tr>
<td>29</td>
<td><strong>int sscanf(const char &#42;str, const char &#42;format, …)</strong> <br> 从字符串读取格式化输入。</td>
</tr>
<tr>
<td>30</td>
<td><strong>int fgetc(FILE &#42;stream)</strong> <br> 从指定的流 stream 获取下一个字符（一个无符号字符），并把位置标识符往前移动。</td>
</tr>
<tr>
<td>31</td>
<td><strong>char &#42;fgets(char &#42;str, int n, FILE &#42;stream)</strong> <br> 从指定的流 stream 读取一行，并把它存储在 str 所指向的字符串内。当读取 (n-1) 个字符时，或者读取到换行符时，或者到达文件末尾时，它会停止，具体视情况而定。</td>
</tr>
<tr>
<td>32</td>
<td><strong>int fputc(int char, FILE &#42;stream)</strong> <br> 把参数 char 指定的字符（一个无符号字符）写入到指定的流 stream 中，并把位置标识符往前移动。</td>
</tr>
<tr>
<td>33</td>
<td><strong>int fputs(const char &#42;str, FILE &#42;stream)</strong> <br> 把字符串写入到指定的流 stream 中，但不包括空字符。</td>
</tr>
<tr>
<td>34</td>
<td><strong>int getc(FILE &#42;stream)</strong> <br> 从指定的流 stream 获取下一个字符（一个无符号字符），并把位置标识符往前移动。</td>
</tr>
<tr>
<td>35</td>
<td><strong>int getchar(void)</strong> <br> 从标准输入 stdin 获取一个字符（一个无符号字符）。</td>
</tr>
<tr>
<td>36</td>
<td><strong>char &#42;gets(char &#42;str)</strong> <br> 从标准输入 stdin 读取一行，并把它存储在 str 所指向的字符串中。当读取到换行符时，或者到达文件末尾时，它会停止，具体视情况而定。</td>
</tr>
<tr>
<td>37</td>
<td><strong>int putc(int char, FILE &#42;stream)</strong> <br> 把参数 char 指定的字符（一个无符号字符）写入到指定的流 stream 中，并把位置标识符往前移动。</td>
</tr>
<tr>
<td>38</td>
<td><strong>int putchar(int char)</strong> <br> 把参数 char 指定的字符（一个无符号字符）写入到标准输出 stdout 中。</td>
</tr>
<tr>
<td>39</td>
<td><strong>int puts(const char &#42;str)</strong> <br> 把一个字符串写入到标准输出 stdout，直到空字符，但不包括空字符。换行符会被追加到输出中。</td>
</tr>
<tr>
<td>40</td>
<td><strong>int ungetc(int char, FILE &#42;stream)</strong> <br> 把字符 char（一个无符号字符）推入到指定的流 stream 中，以便它是下一个被读取到的字符。</td>
</tr>
<tr>
<td>41</td>
<td><strong>void perror(const char &#42;str)</strong> <br> 把一个描述性错误消息输出到标准错误 stderr。首先输出字符串 str，后跟一个冒号，然后是一个空格。</td>
</tr>
</tbody>
</table>
<h2 id="函数用例"><a href="#函数用例" class="headerlink" title="函数用例"></a>函数用例</h2><h3 id="1、fclose"><a href="#1、fclose" class="headerlink" title="1、fclose()"></a>1、fclose()</h3><h4 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 int fclose(FILE *stream) 关闭流 stream。刷新所有的缓冲区。</p>
<h4 id="声明"><a href="#声明" class="headerlink" title="声明"></a>声明</h4><p>下面是 fclose() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int fclose(FILE *stream)</span><br></pre></td></tr></table></figure>
<h4 id="参数"><a href="#参数" class="headerlink" title="参数"></a>参数</h4><ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象指定了要被关闭的流。</li>
</ul>
<h4 id="返回值"><a href="#返回值" class="headerlink" title="返回值"></a>返回值</h4><p>如果流成功关闭，则该方法返回零。如果失败，则返回 EOF。</p>
<h4 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 fclose() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line"> </span><br><span class="line">   fp = fopen(&quot;file.txt&quot;, &quot;w&quot;);</span><br><span class="line"></span><br><span class="line">   fprintf(fp, &quot;%s&quot;, &quot;这里是 单片机助手&quot;);</span><br><span class="line">   fclose(fp);</span><br><span class="line">   </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将创建一个文件 file.txt，然后写入下面的文本行，最后使用 fclose() 函数关闭文件。</p>
<blockquote>
<p>这里是 单片机助手</p>
</blockquote>
<h3 id="2、clearerr"><a href="#2、clearerr" class="headerlink" title="2、clearerr()"></a>2、clearerr()</h3><h4 id="描述-1"><a href="#描述-1" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 void clearerr(FILE *stream) 清除给定流 stream 的文件结束和错误标识符。</p>
<h4 id="声明-1"><a href="#声明-1" class="headerlink" title="声明"></a>声明</h4><p>下面是 clearerr() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">void clearerr(FILE *stream)</span><br></pre></td></tr></table></figure>
<h4 id="参数-1"><a href="#参数-1" class="headerlink" title="参数"></a>参数</h4><ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象标识了流。</li>
</ul>
<h4 id="返回值-1"><a href="#返回值-1" class="headerlink" title="返回值"></a>返回值</h4><p>这不会失败，且不会设置外部变量 errno，但是如果它检测到它的参数不是一个有效的流，则返回 -1，并设置 errno 为 EBADF。</p>
<h4 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 clearerr() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   char c;</span><br><span class="line"></span><br><span class="line">   fp = fopen(&quot;file.txt&quot;, &quot;w&quot;);</span><br><span class="line"></span><br><span class="line">   c = fgetc(fp);</span><br><span class="line">   if( ferror(fp) )</span><br><span class="line">   &#123;</span><br><span class="line">      printf(&quot;读取文件：file.txt 时发生错误\n&quot;);</span><br><span class="line">   &#125;</span><br><span class="line">   clearerr(fp);</span><br><span class="line">   if( ferror(fp) )</span><br><span class="line">   &#123;</span><br><span class="line">      printf(&quot;读取文件：file.txt 时发生错误\n&quot;);</span><br><span class="line">   &#125;</span><br><span class="line">   fclose(fp);</span><br><span class="line"></span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>假设我们有一个文本文件 file.txt，它是一个空文件。让我们编译并运行上面的程序，因为我们试图读取一个以只写模式打开的文件，这将产生以下结果。</p>
<blockquote>
<p>读取文件：file.txt 时发生错误</p>
</blockquote>
<h3 id="3、feof"><a href="#3、feof" class="headerlink" title="3、feof()"></a>3、feof()</h3><h4 id="描述-2"><a href="#描述-2" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 int feof(FILE *stream) 测试给定流 stream 的文件结束标识符。</p>
<h4 id="声明-2"><a href="#声明-2" class="headerlink" title="声明"></a>声明</h4><p>下面是 feof() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int feof(FILE *stream)</span><br></pre></td></tr></table></figure>
<h4 id="参数-2"><a href="#参数-2" class="headerlink" title="参数"></a>参数</h4><ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象标识了流。</li>
</ul>
<h4 id="返回值-2"><a href="#返回值-2" class="headerlink" title="返回值"></a>返回值</h4><p>当设置了与流关联的文件结束标识符时，该函数返回一个非零值，否则返回零。</p>
<h4 id="实例-2"><a href="#实例-2" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 feof() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   int c;</span><br><span class="line">  </span><br><span class="line">   fp = fopen(&quot;file.txt&quot;,&quot;r&quot;);</span><br><span class="line">   if(fp == NULL) </span><br><span class="line">   &#123;</span><br><span class="line">      perror(&quot;打开文件时发生错误&quot;);</span><br><span class="line">      return(-1);</span><br><span class="line">   &#125;</span><br><span class="line">   while(1)</span><br><span class="line">   &#123;</span><br><span class="line">      c = fgetc(fp);</span><br><span class="line">      if( feof(fp) )</span><br><span class="line">      &#123; </span><br><span class="line">          break ;</span><br><span class="line">      &#125;</span><br><span class="line">      printf(&quot;%c&quot;, c);</span><br><span class="line">   &#125;</span><br><span class="line">   fclose(fp);</span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>假设我们有一个文本文件 file.txt，它的内容如下所示。该文件将作为我们实例程序中的一个输入使用：</p>
<blockquote>
<p>这里是 单片机助手</p>
</blockquote>
<p>让我们编译并运行上面的程序，这将产生以下结果：</p>
<blockquote>
<p>这里是 单片机助手</p>
</blockquote>
<h3 id="4、ferror"><a href="#4、ferror" class="headerlink" title="4、ferror()"></a>4、ferror()</h3><h4 id="描述-3"><a href="#描述-3" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 int ferror(FILE *stream) 测试给定流 stream 的错误标识符。</p>
<h4 id="声明-3"><a href="#声明-3" class="headerlink" title="声明"></a>声明</h4><p>下面是 ferror() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int ferror(FILE *stream)</span><br></pre></td></tr></table></figure>
<h4 id="参数-3"><a href="#参数-3" class="headerlink" title="参数"></a>参数</h4><ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象标识了流。</li>
</ul>
<h4 id="返回值-3"><a href="#返回值-3" class="headerlink" title="返回值"></a>返回值</h4><p>如果设置了与流关联的错误标识符，该函数返回一个非零值，否则返回一个零值。</p>
<h4 id="实例-3"><a href="#实例-3" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 ferror() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   char c;</span><br><span class="line"></span><br><span class="line">   fp = fopen(&quot;file.txt&quot;, &quot;w&quot;);</span><br><span class="line"></span><br><span class="line">   c = fgetc(fp);</span><br><span class="line">   if( ferror(fp) )</span><br><span class="line">   &#123;</span><br><span class="line">      printf(&quot;读取文件：file.txt 时发生错误\n&quot;);</span><br><span class="line">   &#125;</span><br><span class="line">   clearerr(fp);</span><br><span class="line">   if( ferror(fp) )</span><br><span class="line">   &#123;</span><br><span class="line">      printf(&quot;读取文件：file.txt 时发生错误\n&quot;);</span><br><span class="line">   &#125;</span><br><span class="line">   fclose(fp);</span><br><span class="line"></span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>假设我们有一个文本文件 file.txt，它是一个空文件。让我们编译并运行上面的程序，因为我们试图读取一个以只写模式打开的文件，这将产生以下结果。</p>
<blockquote>
<p>读取文件：file.txt 时发生错误</p>
</blockquote>
<h3 id="5、fflush"><a href="#5、fflush" class="headerlink" title="5、fflush()"></a>5、fflush()</h3><h4 id="描述-4"><a href="#描述-4" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 int fflush(FILE *stream) 刷新流 stream 的输出缓冲区。</p>
<h4 id="声明-4"><a href="#声明-4" class="headerlink" title="声明"></a>声明</h4><p>下面是 fflush() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int fflush(FILE *stream)</span><br></pre></td></tr></table></figure>
<h4 id="参数-4"><a href="#参数-4" class="headerlink" title="参数"></a>参数</h4><ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象指定了一个缓冲流。</li>
</ul>
<h4 id="返回值-4"><a href="#返回值-4" class="headerlink" title="返回值"></a>返回值</h4><p>如果成功，该函数返回零值。如果发生错误，则返回 EOF，且设置错误标识符（即 feof）。</p>
<h4 id="实例-4"><a href="#实例-4" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 fflush() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;string.h&gt;</span><br><span class="line"> </span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line"> </span><br><span class="line">   char buff[1024];</span><br><span class="line"> </span><br><span class="line">   memset( buff, &apos;\0&apos;, sizeof( buff ));</span><br><span class="line"> </span><br><span class="line">   fprintf(stdout, &quot;启用全缓冲\n&quot;);</span><br><span class="line">   setvbuf(stdout, buff, _IOFBF, 1024);</span><br><span class="line"> </span><br><span class="line">   fprintf(stdout, &quot;这里是 runoob.com\n&quot;);</span><br><span class="line">   fprintf(stdout, &quot;该输出将保存到 buff\n&quot;);</span><br><span class="line">   fflush( stdout );</span><br><span class="line"> </span><br><span class="line">   fprintf(stdout, &quot;这将在编程时出现\n&quot;);</span><br><span class="line">   fprintf(stdout, &quot;最后休眠五秒钟\n&quot;);</span><br><span class="line"> </span><br><span class="line">   sleep(5);</span><br><span class="line"> </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将产生以下结果。在这里，程序把缓冲输出保存到 buff，直到首次调用 fflush() 为止，然后开始缓冲输出，最后休眠 5 秒钟。它会在程序结束之前，发送剩余的输出到 STDOUT。</p>
<blockquote>
<p>启用全缓冲<br>这里是 runoob.com<br>该输出将保存到 buff<br>这将在编程时出现<br>最后休眠五秒钟</p>
</blockquote>
<h3 id="6、fgetpos"><a href="#6、fgetpos" class="headerlink" title="6、fgetpos()"></a>6、fgetpos()</h3><h4 id="描述-5"><a href="#描述-5" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 int fgetpos(FILE <em>stream, fpos_t </em>pos) 获取流 stream 的当前文件位置，并把它写入到 pos。</p>
<h4 id="声明-5"><a href="#声明-5" class="headerlink" title="声明"></a>声明</h4><p>下面是 fgetpos() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int fgetpos(FILE *stream, fpos_t *pos)</span><br></pre></td></tr></table></figure>
<h4 id="参数-5"><a href="#参数-5" class="headerlink" title="参数"></a>参数</h4><ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象标识了流。</li>
<li>pos – 这是指向 fpos_t 对象的指针。</li>
</ul>
<h4 id="返回值-5"><a href="#返回值-5" class="headerlink" title="返回值"></a>返回值</h4><p>如果成功，该函数返回零。如果发生错误，则返回非零值。</p>
<h4 id="实例-5"><a href="#实例-5" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 fgetpos() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   fpos_t position;</span><br><span class="line"></span><br><span class="line">   fp = fopen(&quot;file.txt&quot;,&quot;w+&quot;);</span><br><span class="line">   fgetpos(fp, &amp;position);</span><br><span class="line">   fputs(&quot;Hello, World!&quot;, fp);</span><br><span class="line">  </span><br><span class="line">   fsetpos(fp, &amp;position);</span><br><span class="line">   fputs(&quot;这将覆盖之前的内容&quot;, fp);</span><br><span class="line">   fclose(fp);</span><br><span class="line">   </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将创建一个文件 file.txt，它的内容如下。首先我们使用 fgetpos() 函数获取文件的初始位置，接着我们向文件写入 Hello, World!，然后我们使用 fsetpos() 函数来重置写指针到文件的开头，重写文件为下列内容：</p>
<blockquote>
<p>这将覆盖之前的内容</p>
</blockquote>
<p>现在让我们使用下面的程序查看上面文件的内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   int c;</span><br><span class="line">   int n = 0;</span><br><span class="line"></span><br><span class="line">   fp = fopen(&quot;file.txt&quot;,&quot;r&quot;);</span><br><span class="line">   while(1)</span><br><span class="line">   &#123;</span><br><span class="line">      c = fgetc(fp);</span><br><span class="line">      if( feof(fp) )</span><br><span class="line">      &#123;</span><br><span class="line">          break ;</span><br><span class="line">      &#125;</span><br><span class="line">      printf(&quot;%c&quot;, c);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   fclose(fp);</span><br><span class="line"></span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="7、fopen"><a href="#7、fopen" class="headerlink" title="7、fopen()"></a>7、fopen()</h3><h4 id="描述-6"><a href="#描述-6" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 FILE <em>fopen(const char </em>filename, const char *mode) 使用给定的模式 mode 打开 filename 所指向的文件。</p>
<h4 id="声明-6"><a href="#声明-6" class="headerlink" title="声明"></a>声明</h4><p>下面是 fopen() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">FILE *fopen(const char *filename, const char *mode)</span><br></pre></td></tr></table></figure>
<h4 id="参数-6"><a href="#参数-6" class="headerlink" title="参数"></a>参数</h4><ul>
<li>filename – 这是 C 字符串，包含了要打开的文件名称。</li>
<li>mode – 这是 C 字符串，包含了文件访问模式，模式如下：</li>
</ul>
<table>
<thead>
<tr>
<th>模式</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>“r”</td>
<td>打开一个用于读取的文件。该文件必须存在。</td>
</tr>
<tr>
<td>“w”</td>
<td>创建一个用于写入的空文件。如果文件名称与已存在的文件相同，则会删除已有文件的内容，文件被视为一个新的空文件。</td>
</tr>
<tr>
<td>“a”</td>
<td>追加到一个文件。写操作向文件末尾追加数据。如果文件不存在，则创建文件。</td>
</tr>
<tr>
<td>“r+”</td>
<td>打开一个用于更新的文件，可读取也可写入。该文件必须存在。</td>
</tr>
<tr>
<td>“w+”</td>
<td>创建一个用于读写的空文件。</td>
</tr>
<tr>
<td>“a+”</td>
<td>打开一个用于读取和追加的文件。</td>
</tr>
</tbody>
</table>
<h4 id="返回值-6"><a href="#返回值-6" class="headerlink" title="返回值"></a>返回值</h4><p>该函数返回一个 FILE 指针。否则返回 NULL，且设置全局变量 errno 来标识错误。</p>
<h4 id="实例-6"><a href="#实例-6" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 fopen() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;stdlib.h&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">   FILE * fp;</span><br><span class="line"></span><br><span class="line">   fp = fopen (&quot;file.txt&quot;, &quot;w+&quot;);</span><br><span class="line">   fprintf(fp, &quot;%s %s %s %d&quot;, &quot;We&quot;, &quot;are&quot;, &quot;in&quot;, 2014);</span><br><span class="line">   </span><br><span class="line">   fclose(fp);</span><br><span class="line">   </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将创建一个带有一下内容的文件 file.txt：</p>
<blockquote>
<p>We are in 2014</p>
</blockquote>
<p>现在让我们使用下面的程序查看上面文件的内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   int c;</span><br><span class="line">  </span><br><span class="line">   fp = fopen(&quot;file.txt&quot;,&quot;r&quot;);</span><br><span class="line">   while(1)</span><br><span class="line">   &#123;</span><br><span class="line">      c = fgetc(fp);</span><br><span class="line">      if( feof(fp) )</span><br><span class="line">      &#123; </span><br><span class="line">          break ;</span><br><span class="line">      &#125;</span><br><span class="line">      printf(&quot;%c&quot;, c);</span><br><span class="line">   &#125;</span><br><span class="line">   fclose(fp);</span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="8、fread"><a href="#8、fread" class="headerlink" title="8、fread()"></a>8、fread()</h3><h4 id="描述-7"><a href="#描述-7" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 size_t fread(void <em>ptr, size_t size, size_t nmemb, FILE </em>stream) 从给定流 stream 读取数据到 ptr 所指向的数组中。</p>
<p>####声明</p>
<p>下面是 fread() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)</span><br></pre></td></tr></table></figure>
<h4 id="参数-7"><a href="#参数-7" class="headerlink" title="参数"></a>参数</h4><ul>
<li>ptr – 这是指向带有最小尺寸 size*nmemb 字节的内存块的指针。</li>
<li>size – 这是要读取的每个元素的大小，以字节为单位。</li>
<li>nmemb – 这是元素的个数，每个元素的大小为 size 字节。</li>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象指定了一个输入流。</li>
</ul>
<h4 id="返回值-7"><a href="#返回值-7" class="headerlink" title="返回值"></a>返回值</h4><p>成功读取的元素总数会以 size_t 对象返回，size_t 对象是一个整型数据类型。如果总数与 nmemb 参数不同，则可能发生了一个错误或者到达了文件末尾。</p>
<h4 id="实例-7"><a href="#实例-7" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 fread() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;string.h&gt;</span><br><span class="line"> </span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   char c[] = &quot;This is mcu&quot;;</span><br><span class="line">   char buffer[20];</span><br><span class="line"> </span><br><span class="line">   /* 打开文件用于读写 */</span><br><span class="line">   fp = fopen(&quot;file.txt&quot;, &quot;w+&quot;);</span><br><span class="line"> </span><br><span class="line">   /* 写入数据到文件 */</span><br><span class="line">   fwrite(c, strlen(c) + 1, 1, fp);</span><br><span class="line"> </span><br><span class="line">   /* 查找文件的开头 */</span><br><span class="line">   fseek(fp, 0, SEEK_SET);</span><br><span class="line"> </span><br><span class="line">   /* 读取并显示数据 */</span><br><span class="line">   fread(buffer, strlen(c)+1, 1, fp);</span><br><span class="line">   printf(&quot;%s\n&quot;, buffer);</span><br><span class="line">   fclose(fp);</span><br><span class="line">   </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将创建一个文件 file.txt，然后写入内容 This is runoob。接下来我们使用 fseek() 函数来重置写指针到文件的开头，文件内容如下所示：</p>
<blockquote>
<p>This is mcu</p>
</blockquote>
<h3 id="9、freopen"><a href="#9、freopen" class="headerlink" title="9、freopen()"></a>9、freopen()</h3><h4 id="描述-8"><a href="#描述-8" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 FILE <em>freopen(const char </em>filename, const char <em>mode, FILE </em>stream) 把一个新的文件名 filename 与给定的打开的流 stream 关联，同时关闭流中的旧文件。</p>
<h4 id="声明-7"><a href="#声明-7" class="headerlink" title="声明"></a>声明</h4><p>下面是 freopen() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">FILE *freopen(const char *filename, const char *mode, FILE *stream)</span><br></pre></td></tr></table></figure>
<h4 id="参数-8"><a href="#参数-8" class="headerlink" title="参数"></a>参数</h4><ul>
<li>filename – 这是 C 字符串，包含了要打开的文件名称。</li>
<li>mode – 这是 C 字符串，包含了文件访问模式，模式如下：</li>
</ul>
<table>
<thead>
<tr>
<th>模式</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>“r”</td>
<td>打开一个用于读取的文件。该文件必须存在。</td>
</tr>
<tr>
<td>“w”</td>
<td>创建一个用于写入的空文件。如果文件名称与已存在的文件相同，则会删除已有文件的内容，文件被视为一个新的空文件。</td>
</tr>
<tr>
<td>“a”</td>
<td>追加到一个文件。写操作向文件末尾追加数据。如果文件不存在，则创建文件。</td>
</tr>
<tr>
<td>“r+”</td>
<td>打开一个用于更新的文件，可读取也可写入。该文件必须存在。</td>
</tr>
<tr>
<td>“w+”</td>
<td>创建一个用于读写的空文件。</td>
</tr>
<tr>
<td>“a+”</td>
<td>打开一个用于读取和追加的文件。</td>
</tr>
</tbody>
</table>
<ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象标识了要被重新打开的流。</li>
</ul>
<h4 id="返回值-8"><a href="#返回值-8" class="headerlink" title="返回值"></a>返回值</h4><p>如果文件成功打开，则函数返回一个指针，指向用于标识流的对象。否则，返回空指针。</p>
<h4 id="实例-8"><a href="#实例-8" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 freopen() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line"></span><br><span class="line">   printf(&quot;该文本重定向到 stdout\n&quot;);</span><br><span class="line"></span><br><span class="line">   fp = freopen(&quot;file.txt&quot;, &quot;w+&quot;, stdout);</span><br><span class="line"></span><br><span class="line">   printf(&quot;该文本重定向到 file.txt\n&quot;);</span><br><span class="line"></span><br><span class="line">   fclose(fp);</span><br><span class="line">   </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将发送下列行到标准输出 STDOUT，因为起初我们并没有打开标准输出：</p>
<blockquote>
<p>该文本重定向到 stdout</p>
</blockquote>
<p>在调用 freopen() 之后，它会关联标准输出 STDOUT 到文件 file.txt，无论我们在标准输出 STDOUT 中写了什么都会被写入 file.txt，所以文件 file.txt 将有以下内容。</p>
<blockquote>
<p>该文本重定向到 file.txt</p>
</blockquote>
<p>现在让我们使用下面的程序查看上面文件的内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   int c;</span><br><span class="line"></span><br><span class="line">   fp = fopen(&quot;file.txt&quot;,&quot;r&quot;);</span><br><span class="line">   while(1)</span><br><span class="line">   &#123;</span><br><span class="line">      c = fgetc(fp);</span><br><span class="line">      if( feof(fp) )</span><br><span class="line">      &#123;</span><br><span class="line">          break ;</span><br><span class="line">      &#125;</span><br><span class="line">      printf(&quot;%c&quot;, c);</span><br><span class="line">   &#125;</span><br><span class="line">   fclose(fp);</span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="10、fseek"><a href="#10、fseek" class="headerlink" title="10、fseek()"></a>10、fseek()</h3><h4 id="描述-9"><a href="#描述-9" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 int fseek(FILE *stream, long int offset, int whence) 设置流 stream 的文件位置为给定的偏移 offset，参数 offset 意味着从给定的 whence 位置查找的字节数。</p>
<h4 id="声明-8"><a href="#声明-8" class="headerlink" title="声明"></a>声明</h4><p>下面是 fseek() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int fseek(FILE *stream, long int offset, int whence)</span><br></pre></td></tr></table></figure>
<h4 id="参数-9"><a href="#参数-9" class="headerlink" title="参数"></a>参数</h4><ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象标识了流。</li>
<li>offset – 这是相对 whence 的偏移量，以字节为单位。</li>
<li>whence – 这是表示开始添加偏移 offset 的位置。它一般指定为下列常量之一：</li>
</ul>
<table>
<thead>
<tr>
<th>常量</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>SEEK_SET</td>
<td>文件的开头</td>
</tr>
<tr>
<td>SEEK_CUR</td>
<td>文件指针的当前位置</td>
</tr>
<tr>
<td>SEEK_END</td>
<td>文件的末尾</td>
</tr>
</tbody>
</table>
<h4 id="返回值-9"><a href="#返回值-9" class="headerlink" title="返回值"></a>返回值</h4><p>如果成功，则该函数返回零，否则返回非零值。</p>
<h4 id="实例-9"><a href="#实例-9" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 fseek() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line"></span><br><span class="line">   fp = fopen(&quot;file.txt&quot;,&quot;w+&quot;);</span><br><span class="line">   fputs(&quot;This is runoob.com&quot;, fp);</span><br><span class="line">  </span><br><span class="line">   fseek( fp, 7, SEEK_SET );</span><br><span class="line">   fputs(&quot; C Programming Langauge&quot;, fp);</span><br><span class="line">   fclose(fp);</span><br><span class="line">   </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将创建文件 file.txt，它的内容如下。最初程序创建文件和写入 This is runoob.com，但是之后我们在第七个位置重置了写指针，并使用 puts() 语句来重写文件，内容如下：</p>
<blockquote>
<p>This is C Programming Langauge</p>
</blockquote>
<p>现在让我们使用下面的程序查看上面文件的内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   int c;</span><br><span class="line"></span><br><span class="line">   fp = fopen(&quot;file.txt&quot;,&quot;r&quot;);</span><br><span class="line">   while(1)</span><br><span class="line">   &#123;</span><br><span class="line">      c = fgetc(fp);</span><br><span class="line">      if( feof(fp) )</span><br><span class="line">      &#123;</span><br><span class="line">          break ;</span><br><span class="line">      &#125;</span><br><span class="line">      printf(&quot;%c&quot;, c);</span><br><span class="line">   &#125;</span><br><span class="line">   fclose(fp);</span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="11、fsetpos"><a href="#11、fsetpos" class="headerlink" title="11、fsetpos()"></a>11、fsetpos()</h3><h4 id="描述-10"><a href="#描述-10" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 int fsetpos(FILE <em>stream, const fpos_t </em>pos) 设置给定流 stream 的文件位置为给定的位置。参数 pos 是由函数 fgetpos 给定的位置。</p>
<h4 id="声明-9"><a href="#声明-9" class="headerlink" title="声明"></a>声明</h4><p>下面是 fsetpos() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int fsetpos(FILE *stream, const fpos_t *pos)</span><br></pre></td></tr></table></figure>
<h4 id="参数-10"><a href="#参数-10" class="headerlink" title="参数"></a>参数</h4><ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象标识了流。</li>
<li>pos – 这是指向 fpos_t 对象的指针，该对象包含了之前通过 fgetpos 获得的位置。</li>
</ul>
<h4 id="返回值-10"><a href="#返回值-10" class="headerlink" title="返回值"></a>返回值</h4><p>如果成功，该函数返回零值，否则返回非零值，并设置全局变量 errno 为一个正值，该值可通过 perror 来解释。</p>
<h4 id="实例-10"><a href="#实例-10" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 fsetpos() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   fpos_t position;</span><br><span class="line"></span><br><span class="line">   fp = fopen(&quot;file.txt&quot;,&quot;w+&quot;);</span><br><span class="line">   fgetpos(fp, &amp;position);</span><br><span class="line">   fputs(&quot;Hello, World!&quot;, fp);</span><br><span class="line">  </span><br><span class="line">   fsetpos(fp, &amp;position);</span><br><span class="line">   fputs(&quot;这将覆盖之前的内容&quot;, fp);</span><br><span class="line">   fclose(fp);</span><br><span class="line">   </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将创建一个文件 file.txt，它的内容如下。首先我们使用 fgetpos() 函数获取文件的初始位置，接着我们向文件写入 Hello, World!，然后我们使用 fsetpos() 函数来重置写指针到文件的开头，重写文件为下列内容：</p>
<blockquote>
<p>这将覆盖之前的内容</p>
</blockquote>
<p>现在让我们使用下面的程序查看上面文件的内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   int c;</span><br><span class="line"></span><br><span class="line">   fp = fopen(&quot;file.txt&quot;,&quot;r&quot;);</span><br><span class="line">   while(1)</span><br><span class="line">   &#123;</span><br><span class="line">      c = fgetc(fp);</span><br><span class="line">      if( feof(fp) )</span><br><span class="line">      &#123;</span><br><span class="line">          break ;</span><br><span class="line">      &#125;</span><br><span class="line">      printf(&quot;%c&quot;, c);</span><br><span class="line">   &#125;</span><br><span class="line">   fclose(fp);</span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="12、ftell"><a href="#12、ftell" class="headerlink" title="12、ftell()"></a>12、ftell()</h3><h4 id="描述-11"><a href="#描述-11" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 long int ftell(FILE *stream) 返回给定流 stream 的当前文件位置。</p>
<h4 id="声明-10"><a href="#声明-10" class="headerlink" title="声明"></a>声明</h4><p>下面是 ftell() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">long int ftell(FILE *stream)</span><br></pre></td></tr></table></figure>
<h4 id="参数-11"><a href="#参数-11" class="headerlink" title="参数"></a>参数</h4><ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象标识了流。</li>
</ul>
<h4 id="返回值-11"><a href="#返回值-11" class="headerlink" title="返回值"></a>返回值</h4><p>该函数返回位置标识符的当前值。如果发生错误，则返回 -1L，全局变量 errno 被设置为一个正值。</p>
<h4 id="实例-11"><a href="#实例-11" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 ftell() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   int len;</span><br><span class="line"></span><br><span class="line">   fp = fopen(&quot;file.txt&quot;, &quot;r&quot;);</span><br><span class="line">   if( fp == NULL ) </span><br><span class="line">   &#123;</span><br><span class="line">      perror (&quot;打开文件错误&quot;);</span><br><span class="line">      return(-1);</span><br><span class="line">   &#125;</span><br><span class="line">   fseek(fp, 0, SEEK_END);</span><br><span class="line"></span><br><span class="line">   len = ftell(fp);</span><br><span class="line">   fclose(fp);</span><br><span class="line"></span><br><span class="line">   printf(&quot;file.txt 的总大小 = %d 字节\n&quot;, len);</span><br><span class="line">   </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>假设我们有一个文本文件 file.txt，它的内容如下：</p>
<blockquote>
<p>This is mcu</p>
</blockquote>
<p>让我们编译并运行上面的程序，如果文件内容如上所示，这将产生以下结果，否则会根据文件内容给出不同的结果：</p>
<blockquote>
<p>file.txt 的总大小 = 12 字节</p>
</blockquote>
<h3 id="13、fwrite"><a href="#13、fwrite" class="headerlink" title="13、fwrite()"></a>13、fwrite()</h3><h4 id="描述-12"><a href="#描述-12" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 size_t fwrite(const void <em>ptr, size_t size, size_t nmemb, FILE </em>stream) 把 ptr 所指向的数组中的数据写入到给定流 stream 中。</p>
<h4 id="声明-11"><a href="#声明-11" class="headerlink" title="声明"></a>声明</h4><p>下面是 fwrite() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)</span><br></pre></td></tr></table></figure>
<h4 id="参数-12"><a href="#参数-12" class="headerlink" title="参数"></a>参数</h4><ul>
<li>ptr – 这是指向要被写入的元素数组的指针。</li>
<li>size – 这是要被写入的每个元素的大小，以字节为单位。</li>
<li>nmemb – 这是元素的个数，每个元素的大小为 size 字节。</li>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象指定了一个输出流。</li>
</ul>
<h4 id="返回值-12"><a href="#返回值-12" class="headerlink" title="返回值"></a>返回值</h4><p>如果成功，该函数返回一个 size_t 对象，表示元素的总数，该对象是一个整型数据类型。如果该数字与 nmemb 参数不同，则会显示一个错误。</p>
<h4 id="实例-12"><a href="#实例-12" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 fwrite() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;stdio.h&gt;</span><br><span class="line"> </span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   char str[] = &quot;This is mcu&quot;;</span><br><span class="line"> </span><br><span class="line">   fp = fopen( &quot;file.txt&quot; , &quot;w&quot; );</span><br><span class="line">   fwrite(str, sizeof(str) , 1, fp );</span><br><span class="line"> </span><br><span class="line">   fclose(fp);</span><br><span class="line">  </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将创建一个文件 file.txt，它的内容如下：</p>
<blockquote>
<p>This is mcu</p>
</blockquote>
<p>现在让我们使用下面的程序查看上面文件的内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"> </span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   int c;</span><br><span class="line"> </span><br><span class="line">   fp = fopen(&quot;file.txt&quot;,&quot;r&quot;);</span><br><span class="line">   while(1)</span><br><span class="line">   &#123;</span><br><span class="line">      c = fgetc(fp);</span><br><span class="line">      if( feof(fp) )</span><br><span class="line">      &#123;</span><br><span class="line">          break ;</span><br><span class="line">      &#125;</span><br><span class="line">      printf(&quot;%c&quot;, c);</span><br><span class="line">   &#125;</span><br><span class="line">   fclose(fp);</span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="14、remove"><a href="#14、remove" class="headerlink" title="14、remove()"></a>14、remove()</h3><p>####描述</p>
<p>C 库函数 int remove(const char *filename) 删除给定的文件名 filename，以便它不再被访问。</p>
<h4 id="声明-12"><a href="#声明-12" class="headerlink" title="声明"></a>声明</h4><p>下面是 remove() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int remove(const char *filename)</span><br></pre></td></tr></table></figure>
<h4 id="参数-13"><a href="#参数-13" class="headerlink" title="参数"></a>参数</h4><ul>
<li>filename – 这是 C 字符串，包含了要被删除的文件名称。</li>
</ul>
<h4 id="返回值-13"><a href="#返回值-13" class="headerlink" title="返回值"></a>返回值</h4><p>如果成功，则返回零。如果错误，则返回 -1，并设置 errno。</p>
<h4 id="实例-13"><a href="#实例-13" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 remove() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;string.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   int ret;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   char filename[] = &quot;file.txt&quot;;</span><br><span class="line"></span><br><span class="line">   fp = fopen(filename, &quot;w&quot;);</span><br><span class="line"></span><br><span class="line">   fprintf(fp, &quot;%s&quot;, &quot;这里是 runoob.com&quot;);</span><br><span class="line">   fclose(fp);</span><br><span class="line">   </span><br><span class="line">   ret = remove(filename);</span><br><span class="line"></span><br><span class="line">   if(ret == 0) </span><br><span class="line">   &#123;</span><br><span class="line">      printf(&quot;文件删除成功&quot;);</span><br><span class="line">   &#125;</span><br><span class="line">   else </span><br><span class="line">   &#123;</span><br><span class="line">      printf(&quot;错误：不能删除该文件&quot;);</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>假设我们有一个文本文件 file.txt，它的内容如下。我们将使用上面的程序来删除该文件。让我们编译并运行上面的程序，这将生成下面的消息，且文件被永久删除。</p>
<blockquote>
<p>文件删除成功</p>
</blockquote>
<h3 id="15、rename"><a href="#15、rename" class="headerlink" title="15、rename()"></a>15、rename()</h3><h4 id="描述-13"><a href="#描述-13" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 int rename(const char <em>old_filename, const char </em>new_filename) 把 old_filename 所指向的文件名改为 new_filename。</p>
<h4 id="声明-13"><a href="#声明-13" class="headerlink" title="声明"></a>声明</h4><p>下面是 rename() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int rename(const char *old_filename, const char *new_filename)</span><br></pre></td></tr></table></figure>
<h4 id="参数-14"><a href="#参数-14" class="headerlink" title="参数"></a>参数</h4><ul>
<li>old_filename – 这是 C 字符串，包含了要被重命名/移动的文件名称。</li>
<li>new_filename – 这是 C 字符串，包含了文件的新名称。</li>
</ul>
<h4 id="返回值-14"><a href="#返回值-14" class="headerlink" title="返回值"></a>返回值</h4><p>如果成功，则返回零。如果错误，则返回 -1，并设置 errno。</p>
<h4 id="实例-14"><a href="#实例-14" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 rename() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   int ret;</span><br><span class="line">   char oldname[] = &quot;file.txt&quot;;</span><br><span class="line">   char newname[] = &quot;newfile.txt&quot;;</span><br><span class="line">   </span><br><span class="line">   ret = rename(oldname, newname);</span><br><span class="line"></span><br><span class="line">   if(ret == 0) </span><br><span class="line">   &#123;</span><br><span class="line">      printf(&quot;文件重命名成功&quot;);</span><br><span class="line">   &#125;</span><br><span class="line">   else </span><br><span class="line">   &#123;</span><br><span class="line">      printf(&quot;错误：不能重命名该文件&quot;);</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>假设我们有一个文本文件 file.txt，它的内容如下。我们将使用上面的程序来重命名该文件。让我们编译并运行上面的程序，这将生成下面的消息，且文件被重命名为 newfile.txt 文件。</p>
<blockquote>
<p>文件重命名成功</p>
</blockquote>
<h3 id="16、rewind"><a href="#16、rewind" class="headerlink" title="16、rewind()"></a>16、rewind()</h3><h4 id="描述-14"><a href="#描述-14" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 void rewind(FILE *stream) 设置文件位置为给定流 stream 的文件的开头。</p>
<h4 id="声明-14"><a href="#声明-14" class="headerlink" title="声明"></a>声明</h4><p>下面是 rewind() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">void rewind(FILE *stream)</span><br></pre></td></tr></table></figure>
<h4 id="参数-15"><a href="#参数-15" class="headerlink" title="参数"></a>参数</h4><ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象标识了流。</li>
</ul>
<h4 id="返回值-15"><a href="#返回值-15" class="headerlink" title="返回值"></a>返回值</h4><p>该函数不返回任何值。</p>
<h4 id="实例-15"><a href="#实例-15" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 rewind() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">   char str[] = &quot;This is runoob.com&quot;;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   int ch;</span><br><span class="line"></span><br><span class="line">   /* 首先让我们在文件中写入一些内容 */</span><br><span class="line">   fp = fopen( &quot;file.txt&quot; , &quot;w&quot; );</span><br><span class="line">   fwrite(str , 1 , sizeof(str) , fp );</span><br><span class="line">   fclose(fp);</span><br><span class="line"></span><br><span class="line">   fp = fopen( &quot;file.txt&quot; , &quot;r&quot; );</span><br><span class="line">   while(1)</span><br><span class="line">   &#123;</span><br><span class="line">      ch = fgetc(fp);</span><br><span class="line">      if( feof(fp) )</span><br><span class="line">      &#123;</span><br><span class="line">          break ;</span><br><span class="line">      &#125;</span><br><span class="line">      printf(&quot;%c&quot;, ch);</span><br><span class="line">   &#125;</span><br><span class="line">   rewind(fp);</span><br><span class="line">   printf(&quot;\n&quot;);</span><br><span class="line">   while(1)</span><br><span class="line">   &#123;</span><br><span class="line">      ch = fgetc(fp);</span><br><span class="line">      if( feof(fp) )</span><br><span class="line">      &#123;</span><br><span class="line">          break ;</span><br><span class="line">      &#125;</span><br><span class="line">      printf(&quot;%c&quot;, ch);</span><br><span class="line">     </span><br><span class="line">   &#125;</span><br><span class="line">   fclose(fp);</span><br><span class="line"></span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>假设我们有一个文本文件 file.txt，它的内容如下：</p>
<blockquote>
<p>This is mcu</p>
</blockquote>
<p>让我们编译并运行上面的程序，这将产生以下结果：</p>
<blockquote>
<p>This is mcu<br>This is mcu</p>
</blockquote>
<h3 id="17、setbuf"><a href="#17、setbuf" class="headerlink" title="17、setbuf()"></a>17、setbuf()</h3><h4 id="描述-15"><a href="#描述-15" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 void setbuf(FILE <em>stream, char </em>buffer) 定义流 stream 应如何缓冲。该函数应在与流 stream 相关的文件被打开时，且还未发生任何输入或输出操作之前被调用一次。</p>
<h4 id="声明-15"><a href="#声明-15" class="headerlink" title="声明"></a>声明</h4><p>下面是 setbuf() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">void setbuf(FILE *stream, char *buffer)</span><br></pre></td></tr></table></figure>
<h4 id="参数-16"><a href="#参数-16" class="headerlink" title="参数"></a>参数</h4><ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象标识了一个打开的流。</li>
<li>buffer – 这是分配给用户的缓冲，它的长度至少为 BUFSIZ 字节，BUFSIZ 是一个宏常量，表示数组的长度。</li>
</ul>
<h4 id="返回值-16"><a href="#返回值-16" class="headerlink" title="返回值"></a>返回值</h4><p>该函数不返回任何值。</p>
<h4 id="实例-16"><a href="#实例-16" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 setbuf() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">   char buf[BUFSIZ];</span><br><span class="line"></span><br><span class="line">   setbuf(stdout, buf);</span><br><span class="line">   puts(&quot;This is mcu&quot;);</span><br><span class="line"></span><br><span class="line">   fflush(stdout);</span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将产生以下结果。在这里，程序在即将输出的时候，发送输出到 STDOUT，否则它将缓冲输出。您也可以使用 fflush() 函数来 to 刷新输出。</p>
<blockquote>
<p>This is mcu</p>
</blockquote>
<h3 id="18、setvbuf"><a href="#18、setvbuf" class="headerlink" title="18、setvbuf()"></a>18、setvbuf()</h3><h4 id="描述-16"><a href="#描述-16" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 int setvbuf(FILE <em>stream, char </em>buffer, int mode, size_t size) 定义流 stream 应如何缓冲。</p>
<h4 id="声明-16"><a href="#声明-16" class="headerlink" title="声明"></a>声明</h4><p>下面是 setvbuf() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int setvbuf(FILE *stream, char *buffer, int mode, size_t size)</span><br></pre></td></tr></table></figure>
<h4 id="参数-17"><a href="#参数-17" class="headerlink" title="参数"></a>参数</h4><ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象标识了一个打开的流。</li>
<li>buffer – 这是分配给用户的缓冲。如果设置为 NULL，该函数会自动分配一个指定大小的缓冲。</li>
<li>mode – 这指定了文件缓冲的模式：</li>
</ul>
<table>
<thead>
<tr>
<th>模式</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>_IOFBF</td>
<td>全缓冲：对于输出，数据在缓冲填满时被一次性写入。对于输入，缓冲会在请求输入且缓冲为空时被填充。</td>
</tr>
<tr>
<td>_IOLBF</td>
<td>行缓冲：对于输出，数据在遇到换行符或者在缓冲填满时被写入，具体视情况而定。对于输入，缓冲会在请求输入且缓冲为空时被填充，直到遇到下一个换行符。</td>
</tr>
<tr>
<td>_IONBF</td>
<td>无缓冲：不使用缓冲。每个 I/O 操作都被即时写入。buffer 和 size 参数被忽略。</td>
</tr>
</tbody>
</table>
<ul>
<li>size –这是缓冲的大小，以字节为单位。</li>
</ul>
<h4 id="返回值-17"><a href="#返回值-17" class="headerlink" title="返回值"></a>返回值</h4><p>如果成功，则该函数返回 0，否则返回非零值。</p>
<h4 id="实例-17"><a href="#实例-17" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 setvbuf() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">   char buff[1024];</span><br><span class="line"></span><br><span class="line">   memset( buff, &apos;\0&apos;, sizeof( buff ));</span><br><span class="line"></span><br><span class="line">   fprintf(stdout, &quot;启用全缓冲\n&quot;);</span><br><span class="line">   setvbuf(stdout, buff, _IOFBF, 1024);</span><br><span class="line"></span><br><span class="line">   fprintf(stdout, &quot;这里是 单片机助手\n&quot;);</span><br><span class="line">   fprintf(stdout, &quot;该输出将保存到 buff\n&quot;);</span><br><span class="line">   fflush( stdout );</span><br><span class="line"></span><br><span class="line">   fprintf(stdout, &quot;这将在编程时出现\n&quot;);</span><br><span class="line">   fprintf(stdout, &quot;最后休眠五秒钟\n&quot;);</span><br><span class="line"></span><br><span class="line">   sleep(5);</span><br><span class="line"></span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将产生以下结果。在这里，程序把缓冲输出保存到 buff，直到首次调用 fflush() 为止，然后开始缓冲输出，最后休眠 5 秒钟。它会在程序结束之前，发送剩余的输出到 STDOUT。</p>
<blockquote>
<p>启用全缓冲<br>这里是 单片机助手<br>该输出将保存到 buff<br>这将在编程时出现<br>最后休眠五秒钟</p>
</blockquote>
<h3 id="19、tmpfile"><a href="#19、tmpfile" class="headerlink" title="19、tmpfile()"></a>19、tmpfile()</h3><h4 id="描述-17"><a href="#描述-17" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 FILE *tmpfile(void) 以二进制更新模式(wb+)创建临时文件。被创建的临时文件会在流关闭的时候或者在程序终止的时候自动删除。</p>
<h4 id="声明-17"><a href="#声明-17" class="headerlink" title="声明"></a>声明</h4><p>下面是 tmpfile() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">FILE *tmpfile(void)</span><br></pre></td></tr></table></figure>
<h4 id="参数-18"><a href="#参数-18" class="headerlink" title="参数"></a>参数</h4><p>NA</p>
<h4 id="返回值-18"><a href="#返回值-18" class="headerlink" title="返回值"></a>返回值</h4><p>如果成功，该函数返回一个指向被创建的临时文件的流指针。如果文件未被创建，则返回 NULL。</p>
<h4 id="实例-18"><a href="#实例-18" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 tmpfile() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line"></span><br><span class="line">   fp = tmpfile();</span><br><span class="line">   printf(&quot;临时文件被创建\n&quot;);</span><br><span class="line"></span><br><span class="line">   /* 您可以在这里使用临时文件 */</span><br><span class="line"></span><br><span class="line">   fclose(fp);</span><br><span class="line"></span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，它将在 /tmp 文件夹中创建一个临时文件，但是一旦程序退出，临时文件会被自动删除，且程序会产生以下结果：</p>
<blockquote>
<p>临时文件被创建</p>
</blockquote>
<h3 id="20、tmpnam"><a href="#20、tmpnam" class="headerlink" title="20、tmpnam()"></a>20、tmpnam()</h3><h4 id="描述-18"><a href="#描述-18" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 char <em>tmpnam(char </em>str) 生成并返回一个有效的临时文件名，该文件名之前是不存在的。如果 str 为空，则只会返回临时文件名。</p>
<h4 id="声明-18"><a href="#声明-18" class="headerlink" title="声明"></a>声明</h4><p>下面是 tmpnam() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">char *tmpnam(char *str)</span><br></pre></td></tr></table></figure>
<h4 id="参数-19"><a href="#参数-19" class="headerlink" title="参数"></a>参数</h4><ul>
<li>str – 这是一个指向字符数组的指针，其中，临时文件名将被存储为 C 字符串。</li>
</ul>
<h4 id="返回值-19"><a href="#返回值-19" class="headerlink" title="返回值"></a>返回值</h4><p>一个指向 C 字符串的指针，该字符串存储了临时文件名。如果 str 是一个空指针，则该指针指向一个内部缓冲区，缓冲区在下一次调用函数时被覆盖。<br>如果 str 不是一个空指针，则返回 str。如果函数未能成功创建可用的文件名，则返回一个空指针。</p>
<h4 id="实例-19"><a href="#实例-19" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 tmpnam() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">   char buffer[L_tmpnam];</span><br><span class="line">   char *ptr;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">   tmpnam(buffer);</span><br><span class="line">   printf(&quot;临时名称 1: %s\n&quot;, buffer);</span><br><span class="line"> </span><br><span class="line">   ptr = tmpnam(NULL);</span><br><span class="line">   printf(&quot;临时名称 2: %s\n&quot;, ptr);</span><br><span class="line"></span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将产生以下结果：</p>
<blockquote>
<p>临时名称 1: /tmp/filebaalTb<br>临时名称 2: /tmp/filedCIbb0</p>
</blockquote>
<h3 id="21、fprintf"><a href="#21、fprintf" class="headerlink" title="21、fprintf()"></a>21、fprintf()</h3><h4 id="描述-19"><a href="#描述-19" class="headerlink" title="描述"></a>描述</h4><p>C 库函数 int fprintf(FILE <em>stream, const char </em>format, …) 发送格式化输出到流 stream 中。</p>
<h4 id="声明-19"><a href="#声明-19" class="headerlink" title="声明"></a>声明</h4><p>下面是 fprintf() 函数的声明。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int fprintf(FILE *stream, const char *format, ...)</span><br></pre></td></tr></table></figure>
<h4 id="参数-20"><a href="#参数-20" class="headerlink" title="参数"></a>参数</h4><ul>
<li>stream – 这是指向 FILE 对象的指针，该 FILE 对象标识了流。</li>
<li>format – 这是 C 字符串，包含了要被写入到流 stream 中的文本。它可以包含嵌入的 format 标签，format 标签可被随后的附加参数中指定的值替换，并按需求进行格式化。format 标签属性是 %[flags][width][.precision][length]specifier，具体讲解如下：</li>
</ul>
<table>
<thead>
<tr>
<th>specifier（说明符）</th>
<th>输出</th>
</tr>
</thead>
<tbody>
<tr>
<td>c</td>
<td>字符</td>
</tr>
<tr>
<td>d 或 i</td>
<td>有符号十进制整数</td>
</tr>
<tr>
<td>e</td>
<td>使用 e 字符的科学科学记数法（尾数和指数）</td>
</tr>
<tr>
<td>E</td>
<td>使用 E 字符的科学科学记数法（尾数和指数）</td>
</tr>
<tr>
<td>f</td>
<td>十进制浮点数</td>
</tr>
<tr>
<td>g</td>
<td>自动选择 %e 或 %f 中合适的表示法</td>
</tr>
<tr>
<td>G</td>
<td>自动选择 %E 或 %f 中合适的表示法</td>
</tr>
<tr>
<td>o</td>
<td>有符号八进制</td>
</tr>
<tr>
<td>s</td>
<td>字符的字符串</td>
</tr>
<tr>
<td>u</td>
<td>无符号十进制整数</td>
</tr>
<tr>
<td>x</td>
<td>无符号十六进制整数</td>
</tr>
<tr>
<td>X</td>
<td>无符号十六进制整数（大写字母）</td>
</tr>
<tr>
<td>p</td>
<td>指针地址</td>
</tr>
<tr>
<td>n</td>
<td>无输出</td>
</tr>
<tr>
<td>%</td>
<td>字符</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>flags（标识）</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>-</td>
<td>在给定的字段宽度内左对齐，默认是右对齐（参见 width 子说明符）。</td>
</tr>
<tr>
<td>+</td>
<td>强制在结果之前显示加号或减号（+ 或 -），即正数前面会显示 + 号。默认情况下，只有负数前面会显示一个 - 号。</td>
</tr>
<tr>
<td>(space)</td>
<td>如果没有写入任何符号，则在该值前面插入一个空格。</td>
</tr>
<tr>
<td>#</td>
<td>与 o、x 或 X 说明符一起使用时，非零值前面会分别显示 0、0x 或 0X。 <br> 与 e、E 和 f 一起使用时，会强制输出包含一个小数点，即使后边没有数字时也会显示小数点。默认情况下，如果后边没有数字时候，不会显示显示小数点。 <br> 与 g 或 G 一起使用时，结果与使用 e 或 E 时相同，但是尾部的零不会被移除。</td>
</tr>
<tr>
<td>0</td>
<td>在指定填充 padding 的数字左边放置零（0），而不是空格（参见 width 子说明符）。</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>width（宽度）</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>(number)</td>
<td>要输出的字符的最小数目。如果输出的值短于该数，结果会用空格填充。如果输出的值长于该数，结果不会被截断。</td>
</tr>
<tr>
<td>*</td>
<td>宽度在 format 字符串中未指定，但是会作为附加整数值参数放置于要被格式化的参数之前。</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>.precision（精度）</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>.number</td>
<td>对于整数说明符（d、i、o、u、x、X）：precision 指定了要写入的数字的最小位数。如果写入的值短于该数，结果会用前导零来填充。如果写入的值长于该数，结果不会被截断。精度为 0 意味着不写入任何字符。 <br> 对于 e、E 和 f 说明符：要在小数点后输出的小数位数。 <br>对于 g 和 G 说明符：要输出的最大有效位数。 <br> 对于 s: 要输出的最大字符数。默认情况下，所有字符都会被输出，直到遇到末尾的空字符。<br> 对于 c 类型：没有任何影响。 <br> 当未指定任何精度时，默认为 1。如果指定时不带有一个显式值，则假定为 0。</td>
</tr>
<tr>
<td>.*</td>
<td>精度在 format 字符串中未指定，但是会作为附加整数值参数放置于要被格式化的参数之前。</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>length（长度）</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>h</td>
<td>参数被解释为短整型或无符号短整型（仅适用于整数说明符：i、d、o、u、x 和 X）。</td>
</tr>
<tr>
<td>l</td>
<td>参数被解释为长整型或无符号长整型，适用于整数说明符（i、d、o、u、x 和 X）及说明符 c（表示一个宽字符）和 s（表示宽字符字符串）。</td>
</tr>
<tr>
<td>L</td>
<td>参数被解释为长双精度型（仅适用于浮点数说明符：e、E、f、g 和 G）。</td>
</tr>
</tbody>
</table>
<ul>
<li><strong>附加参数</strong> – 根据不同的 format 字符串，函数可能需要一系列的附加参数，每个参数包含了一个要被插入的值，替换了 format 参数中指定的每个 % 标签。参数的个数应与 % 标签的个数相同。</li>
</ul>
<h4 id="返回值-20"><a href="#返回值-20" class="headerlink" title="返回值"></a>返回值</h4><p>如果成功，则返回写入的字符总数，否则返回一个负数。</p>
<h4 id="实例-20"><a href="#实例-20" class="headerlink" title="实例"></a>实例</h4><p>下面的实例演示了 fprintf() 函数的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;stdlib.h&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">   FILE * fp;</span><br><span class="line"></span><br><span class="line">   fp = fopen (&quot;file.txt&quot;, &quot;w+&quot;);</span><br><span class="line">   fprintf(fp, &quot;%s %s %s %d&quot;, &quot;We&quot;, &quot;are&quot;, &quot;in&quot;, 2014);</span><br><span class="line">   </span><br><span class="line">   fclose(fp);</span><br><span class="line">   </span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>让我们编译并运行上面的程序，这将创建文件 file.txt，它的内容如下：</p>
<blockquote>
<p>We are in 2014</p>
</blockquote>
<p>现在让我们使用下面的程序查看上面文件的内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">   FILE *fp;</span><br><span class="line">   int c;</span><br><span class="line"></span><br><span class="line">   fp = fopen(&quot;file.txt&quot;,&quot;r&quot;);</span><br><span class="line">   while(1)</span><br><span class="line">   &#123;</span><br><span class="line">      c = fgetc(fp);</span><br><span class="line">      if( feof(fp) )</span><br><span class="line">      &#123;</span><br><span class="line">          break ;</span><br><span class="line">      &#125;</span><br><span class="line">      printf(&quot;%c&quot;, c);</span><br><span class="line">   &#125;</span><br><span class="line">   fclose(fp);</span><br><span class="line">   return(0);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2015/03/11/cfunc11/" rel="next" title="stddef.h">
                <i class="fa fa-chevron-left"></i> stddef.h
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2015/03/13/cfunc13/" rel="prev" title="stdlib.h">
                stdlib.h <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#简介"><span class="nav-number">1.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#库变量"><span class="nav-number">2.</span> <span class="nav-text">库变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#库宏"><span class="nav-number">3.</span> <span class="nav-text">库宏</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#库函数"><span class="nav-number">4.</span> <span class="nav-text">库函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数用例"><span class="nav-number">5.</span> <span class="nav-text">函数用例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、fclose"><span class="nav-number">5.1.</span> <span class="nav-text">1、fclose()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述"><span class="nav-number">5.1.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明"><span class="nav-number">5.1.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数"><span class="nav-number">5.1.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值"><span class="nav-number">5.1.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例"><span class="nav-number">5.1.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2、clearerr"><span class="nav-number">5.2.</span> <span class="nav-text">2、clearerr()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-1"><span class="nav-number">5.2.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-1"><span class="nav-number">5.2.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-1"><span class="nav-number">5.2.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-1"><span class="nav-number">5.2.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-1"><span class="nav-number">5.2.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3、feof"><span class="nav-number">5.3.</span> <span class="nav-text">3、feof()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-2"><span class="nav-number">5.3.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-2"><span class="nav-number">5.3.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-2"><span class="nav-number">5.3.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-2"><span class="nav-number">5.3.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-2"><span class="nav-number">5.3.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4、ferror"><span class="nav-number">5.4.</span> <span class="nav-text">4、ferror()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-3"><span class="nav-number">5.4.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-3"><span class="nav-number">5.4.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-3"><span class="nav-number">5.4.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-3"><span class="nav-number">5.4.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-3"><span class="nav-number">5.4.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5、fflush"><span class="nav-number">5.5.</span> <span class="nav-text">5、fflush()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-4"><span class="nav-number">5.5.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-4"><span class="nav-number">5.5.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-4"><span class="nav-number">5.5.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-4"><span class="nav-number">5.5.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-4"><span class="nav-number">5.5.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6、fgetpos"><span class="nav-number">5.6.</span> <span class="nav-text">6、fgetpos()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-5"><span class="nav-number">5.6.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-5"><span class="nav-number">5.6.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-5"><span class="nav-number">5.6.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-5"><span class="nav-number">5.6.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-5"><span class="nav-number">5.6.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7、fopen"><span class="nav-number">5.7.</span> <span class="nav-text">7、fopen()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-6"><span class="nav-number">5.7.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-6"><span class="nav-number">5.7.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-6"><span class="nav-number">5.7.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-6"><span class="nav-number">5.7.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-6"><span class="nav-number">5.7.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8、fread"><span class="nav-number">5.8.</span> <span class="nav-text">8、fread()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-7"><span class="nav-number">5.8.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-7"><span class="nav-number">5.8.2.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-7"><span class="nav-number">5.8.3.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-7"><span class="nav-number">5.8.4.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9、freopen"><span class="nav-number">5.9.</span> <span class="nav-text">9、freopen()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-8"><span class="nav-number">5.9.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-7"><span class="nav-number">5.9.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-8"><span class="nav-number">5.9.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-8"><span class="nav-number">5.9.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-8"><span class="nav-number">5.9.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#10、fseek"><span class="nav-number">5.10.</span> <span class="nav-text">10、fseek()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-9"><span class="nav-number">5.10.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-8"><span class="nav-number">5.10.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-9"><span class="nav-number">5.10.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-9"><span class="nav-number">5.10.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-9"><span class="nav-number">5.10.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#11、fsetpos"><span class="nav-number">5.11.</span> <span class="nav-text">11、fsetpos()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-10"><span class="nav-number">5.11.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-9"><span class="nav-number">5.11.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-10"><span class="nav-number">5.11.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-10"><span class="nav-number">5.11.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-10"><span class="nav-number">5.11.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#12、ftell"><span class="nav-number">5.12.</span> <span class="nav-text">12、ftell()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-11"><span class="nav-number">5.12.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-10"><span class="nav-number">5.12.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-11"><span class="nav-number">5.12.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-11"><span class="nav-number">5.12.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-11"><span class="nav-number">5.12.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#13、fwrite"><span class="nav-number">5.13.</span> <span class="nav-text">13、fwrite()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-12"><span class="nav-number">5.13.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-11"><span class="nav-number">5.13.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-12"><span class="nav-number">5.13.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-12"><span class="nav-number">5.13.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-12"><span class="nav-number">5.13.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#14、remove"><span class="nav-number">5.14.</span> <span class="nav-text">14、remove()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-12"><span class="nav-number">5.14.1.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-13"><span class="nav-number">5.14.2.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-13"><span class="nav-number">5.14.3.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-13"><span class="nav-number">5.14.4.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#15、rename"><span class="nav-number">5.15.</span> <span class="nav-text">15、rename()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-13"><span class="nav-number">5.15.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-13"><span class="nav-number">5.15.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-14"><span class="nav-number">5.15.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-14"><span class="nav-number">5.15.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-14"><span class="nav-number">5.15.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#16、rewind"><span class="nav-number">5.16.</span> <span class="nav-text">16、rewind()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-14"><span class="nav-number">5.16.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-14"><span class="nav-number">5.16.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-15"><span class="nav-number">5.16.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-15"><span class="nav-number">5.16.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-15"><span class="nav-number">5.16.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#17、setbuf"><span class="nav-number">5.17.</span> <span class="nav-text">17、setbuf()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-15"><span class="nav-number">5.17.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-15"><span class="nav-number">5.17.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-16"><span class="nav-number">5.17.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-16"><span class="nav-number">5.17.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-16"><span class="nav-number">5.17.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#18、setvbuf"><span class="nav-number">5.18.</span> <span class="nav-text">18、setvbuf()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-16"><span class="nav-number">5.18.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-16"><span class="nav-number">5.18.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-17"><span class="nav-number">5.18.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-17"><span class="nav-number">5.18.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-17"><span class="nav-number">5.18.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#19、tmpfile"><span class="nav-number">5.19.</span> <span class="nav-text">19、tmpfile()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-17"><span class="nav-number">5.19.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-17"><span class="nav-number">5.19.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-18"><span class="nav-number">5.19.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-18"><span class="nav-number">5.19.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-18"><span class="nav-number">5.19.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#20、tmpnam"><span class="nav-number">5.20.</span> <span class="nav-text">20、tmpnam()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-18"><span class="nav-number">5.20.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-18"><span class="nav-number">5.20.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-19"><span class="nav-number">5.20.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-19"><span class="nav-number">5.20.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-19"><span class="nav-number">5.20.5.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#21、fprintf"><span class="nav-number">5.21.</span> <span class="nav-text">21、fprintf()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#描述-19"><span class="nav-number">5.21.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#声明-19"><span class="nav-number">5.21.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数-20"><span class="nav-number">5.21.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回值-20"><span class="nav-number">5.21.4.</span> <span class="nav-text">返回值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例-20"><span class="nav-number">5.21.5.</span> <span class="nav-text">实例</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
